﻿using System;
using System.Collections.Concurrent;

namespace UIShell.OSGI.Mvc
{
    class ControllerTypeCache
    {
        private static readonly ConcurrentDictionary<string, Type> ControllerTypes 
            = new ConcurrentDictionary<string, Type>();

        private const string KeyFormat = "{0}.{1}";

        public static void AddControllerType(string pluginName, string controllerName, Type controllerType)
        {
            ControllerTypes.AddOrUpdate(
                string.Format(KeyFormat, pluginName, controllerName.ToLower()), 
                key => controllerType,
                (key, existing) => controllerType);
        }

        public static Type GetControllerType(string pluginName, string controllerName)
        {
            Type result;
            ControllerTypes.TryGetValue(string.Format(KeyFormat, pluginName, controllerName.ToLower()), out result);

            return result;
        }
    }
}
